﻿#ifndef RADIOBUTTON_H
#define RADIOBUTTON_H

#include <QWidget>
#include <QVector>
#include <QToolButton>
#include <QBoxLayout>
#include <QStyleOption>
#include <QPainter>

// 单选按钮
class RadioButton : public QWidget
{
    Q_OBJECT

    // 布局方向：横向或纵向
    Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation NOTIFY orientationChanged)
    // 按钮间距
    Q_PROPERTY(int spacing READ spacing WRITE setSpacing NOTIFY spacingChanged)
    // 当前选中的按钮索引
    Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged)

public:
    explicit RadioButton(QWidget *parent = nullptr);

    // 设置布局方向
    void setOrientation(Qt::Orientation orientation);
    Qt::Orientation orientation() const;

    // 设置按钮间距
    void setSpacing(int spacing);
    int spacing() const;

    // 添加按钮文本
    void addButton(const QString &text);
    void addButtons(const QStringList &texts);

    // 获取/设置当前选中索引
    int currentIndex() const;
    void setCurrentIndex(int index);

    // 获取按钮数量
    int count() const;

signals:
    void currentIndexChanged(int index);
    void orientationChanged(Qt::Orientation orientation);
    void spacingChanged(int spacing);
    void buttonClicked(int index); // 点击按钮时发出信号

protected:
    void paintEvent(QPaintEvent *event) override;

private slots:
    void onButtonClicked();

private:
    QVector<QToolButton*> m_selectedBtnList;
    int m_currentSelectedBtnIndex;
    Qt::Orientation m_orientation;
    int m_spacing;
    QBoxLayout *m_layout;

    void setupButton(QToolButton *button);
    void updateButtonStyles();
};



#endif // RADIOBUTTON_H
